/**
 * 
 * Opened Touchable RIA Framework - A Web RIA Javascript Framework
 * 
 * Copyright (c) 2012
 * Dual licensed under the MIT or GPL v2 licenses.
 * 
 * @author  CuTe_CuBe
 * @version $Id: vml.js 5 2012-03-09 10:44:54Z CC.HGFeng@gmail.com $
 */


(function(){

var a = OTR.Graph.Vml;

/**
 * OTR.Graph.VML.Rect
 * 实现canvas矩形绘画功能 
 * 
 * @class OTR.Graph.VML.Rect
 * @constructor
 */
a.Rect = OTR.extend(OTR.Graph.Brush, {
    /**
     * canvas 绘制矩形实现方法
     * 
     * @return void
     */
    draw: function(){
        var d  = this._adapter.getDoc(),
            s  = this._shape,
            ns = this._adapter.getNs();
        
        var br = s.attr('borderRadius'),
            tag,
            tagName = br == 0 ? 'Rect' : 'RoundRect';

        tag = d.createElement(ns + ':' + tagName),
        cssText = [
               'behavior:url(#default#VML);display:inline-block',
               'position:absolute',
               'width:' + s.attr('width') + 'px',
               'height:' + s.attr('height') + 'px',
               'left:' + s.attr('x') + 'px',
               'top:' + s.attr('y') + 'px'
        ].join(';');
        tag.style.cssText = cssText;
        tag.fillcolor = s.attr('background');
        
        if (br != 0) {
            tag.arcsize = br / 100;
        }
        
        this._adapter.getCanvas().appendChild(tag);
    }
});

/**
 * 线段绘画实现
 * 
 * @class OTR.Graph.Vml.Cycle
 * @construct
 */
a.Cycle = OTR.extend(OTR.Graph.Brush, {
    /**
     * 绘画实现方法
     * 
     * @return void
     */
    draw: function() {
        var d  = this._adapter.getDoc(),
            s  = this._shape,
            ns = this._adapter.getNs();
        
        var tag = d.createElement(ns + ':oval'),
        cssText = [
               'behavior:url(#default#VML);display:inline-block',
               'position:absolute',
               'width:' + s.attr('width') + 'px',
               'height:' + s.attr('height') + 'px',
               'left:' + s.attr('x') + 'px',
               'top:' + s.attr('y') + 'px',
        ].join(';');
        tag.style.cssText = cssText;
        tag.fillcolor = s.attr('background');
        
        this._adapter.getCanvas().appendChild(tag);
    }
});

/**
 * 线段绘画实现
 * 
 * @class OTR.Graph.Vml.Line
 * @construct
 */
a.Line = OTR.extend(OTR.Graph.Brush, {
    /**
     * 绘画实现方法
     * 
     * @return void
     */
    draw: function() {
        var d  = this._adapter.getDoc(),
        s  = this._shape,
        ns = this._adapter.getNs();
    
        var tag = d.createElement(ns + ':line'),
            stroke = d.createElement(ns + ':stroke');
        var cssText = [
               'behavior:url(#default#VML);display:inline-block',
               'position:absolute'
        ].join(';');
        tag.style.cssText = cssText;
        tag.from = s.attr('x') + ',' + s.attr('y');
        tag.to   = s.attr('dx') + ',' + s.attr('dy');
        
        // 样式设置
        tag.strokecolor = s.attr('borderColor');
        
        var sa = s.attr('startArrow'),
            ea = s.attr('endArrow');
        if (sa) {
            stroke.setAttribute('StartArrow', sa);
        }
        if (ea) {
            stroke.setAttribute('EndArrow', ea);
        }
        stroke.style.cssText = 'behavior:url(#default#VML);display:inline-block';
        
        tag.appendChild(stroke);
        
        this._adapter.getCanvas().appendChild(tag);
    }
});

/**
 * 线段绘画实现
 * 
 * @class OTR.Graph.Vml.PolyLine
 * @construct
 */
a.PolyLine = OTR.extend(OTR.Graph.Brush, {
    /**
     * 绘画实现方法
     * 
     * @return void
     */
    draw: function() {
        var d  = this._adapter.getDoc(),
            s  = this._shape,
            ns = this._adapter.getNs(),
            points = s.attr('points');
        
        if (!points || points.length < 2) {
            return ;
        }
    
        var tag = d.createElement(ns + ':polyline'),
            stroke = d.createElement(ns + ':stroke');
        var cssText = [
               'behavior:url(#default#VML);display:inline-block',
               'position:absolute'
        ].join(';');
        tag.style.cssText = cssText;
        var i = 0, k = points.length, p = [];
        for ( ; i < k; i++) {
            p.push(points[i].x + ',' + points[i].y);
        }
        tag.points = p.join(' ');
        
        // 样式设置
        tag.strokecolor  = s.attr('borderColor');
        tag.strokeWeight = s.attr('borderWeight');
        
        var sa = s.attr('startArrow'),
            ea = s.attr('endArrow');
        if (sa) {
            stroke.setAttribute('StartArrow', sa);
        }
        if (ea) {
            stroke.setAttribute('EndArrow', ea);
        }
        stroke.style.cssText = 'behavior:url(#default#VML);display:inline-block';
        
        tag.appendChild(stroke);
        
        this._adapter.getCanvas().appendChild(tag);
    }
});


/**
 * 线段绘画实现
 * 
 * @class OTR.Graph.Vml.Polygon
 * @construct
 */
a.Polygon = OTR.extend(OTR.Graph.Brush, {
    /**
     * 多边形实现方法
     * 
     * @return void
     */
    draw: function() {
        var d  = this._adapter.getDoc(),
        s  = this._shape,
        ns = this._adapter.getNs(),
        points = s.getPoints();
    
        if (!points || points.length < 2) {
            return ;
        }
    
        var tag = d.createElement(ns + ':polyline');
        var cssText = [
               'behavior:url(#default#VML);display:inline-block',
               'position:absolute'
        ].join(';');
        tag.style.cssText = cssText;
        var i = 0, k = points.length, p = [];
        for ( ; i < k; i++) {
            p.push(points[i].x + ',' + points[i].y);
        }
        p.push(points[0].x, points[0].y);
        tag.points = p.join(' ');
        
        // 样式设置
        tag.strokecolor  = s.attr('borderColor');
        tag.strokeWeight = s.attr('borderWeight');
        tag.fillColor    = s.attr('background');

        this._adapter.getCanvas().appendChild(tag);
    }
});



/**
 * 文字绘画实现
 * 
 * @class OTR.Graph.Vml.Text
 * @construct
 */
a.Text = OTR.extend(OTR.Graph.Brush, {
    /**
     * 绘画实现方法
     * 
     * @return void
     */
    draw: function() {
        var s  = this._shape,
            text = s.attr('text'),
            lw   = s.attr('lineWidth');
        
        var d  = this._adapter.getDoc(),
        s  = this._shape,
        ns = this._adapter.getNs();
    
        var tag = d.createElement(ns + ':textbox'),
        cssText = [
               'behavior:url(#default#VML);display:inline-block',
               'position:absolute',
               'font-size:' + s.attr('fontSize'),
               'font-family:' + s.attr('fontFamily'),
               'color:' + s.attr('color'),
               'left:' + s.attr('x') + 'px',
               'top:' + s.attr('y') + 'px',
        ];
        if (lw > 0) {
            css.Text.push('overflow:hidden; width: ' + lw + 'px');
        }
        
        tag.style.cssText = cssText.join(';');
        
        tag.innerText = text;
        
        this._adapter.getCanvas().appendChild(tag);
    }
});
})();